@extends('admin::layouts.master')
@section('title')
    权限列表
@endsection
@section('tab')

    <div id="add_html" style="display:none;">
        <form class="layui-form" lay-filter="permission_form">
            <div class="layui-form-item">
                <label for="L_name" class="layui-form-label">
                    <span class="x-red">*</span>权限名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_name" name="name" required="" lay-verify="name"  autocomplete="off" class="layui-input" maxlength="80">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_display_name" class="layui-form-label">
                    <span class="x-red"></span>权限别名</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_display_name" name="display_name" lay-verify="display_name"  autocomplete="off" class="layui-input" maxlength="80">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_route" class="layui-form-label">
                    <span class="x-red">*</span>路由</label>
                <div class="layui-input-inline">
                    <input type="hidden" id="L_route" name="model" required="" lay-verify="model" autocomplete="off" class="layui-input">
                    <input type="text" id="L_route" name="route" required="" lay-verify="route" autocomplete="off" class="layui-input" maxlength="80">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_alias" class="layui-form-label">
                    <span class="x-red">*</span>路由别名</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_alias" name="alias" required="" lay-verify="alias"  autocomplete="off" class="layui-input"  maxlength="80">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_description" class="layui-form-label">
                    <span class="x-red"></span>权限描述</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_description" name="description" lay-verify="description" autocomplete="off" class="layui-input"  maxlength="150">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_ico" class="layui-form-label">
                    <span class="x-red"></span>图标</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_ico" name="ico" lay-verify="ico" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_sort" class="layui-form-label">
                    <span class="x-red"></span>排序</label>
                <div class="layui-input-inline">
                    <input type="number" id="L_sort" name="sort" lay-verify="sort" autocomplete="off" class="layui-input">
                    <input type="hidden" id="L_pid" name="pid" value="{{$n_id}}">

                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>不填写默认 500
                </div>
            </div>

            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>权限类型</label>
                <div class="layui-input-inline">
                    <select name="is_menu">
                        <option value="1">菜单</option>
                        <option value="2">节点</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"></label>
                <input type="hidden" id="L_id" name="id">
                <button class="layui-btn"   lay-filter="permission_form_submit" lay-submit="">确定</button>
            </div>
        </form>
    </div>

    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">

                    <div class="layui-card-body">
                        <button class="layui-btn layui-btn-normal" onclick="addTopNode();"><i class="layui-icon"></i>添加顶级权限</button>

                        <a class="layui-btn" onclick="location.reload()">
                            <i class="layui-icon layui-icon-refresh"></i>
                        </a>
                        <span class="layui-btn btn-danger" onclick="open_unicode()">图标</span>
                        <span style="float: right">
                            <img src="/admin/lib/zTree/css/zTreeStyle/img/diy/menu.png" alt="">菜单
                            <img src="/admin/lib/zTree/css/zTreeStyle/img/diy/menu_disabled.png" alt="">菜单(已禁用）
                            <img src="/admin/lib/zTree/css/zTreeStyle/img/diy/point.png" alt="">节点
                            <img src="/admin/lib/zTree/css/zTreeStyle/img/diy/point_disabled.png" alt="">节点(已禁用）
                        </span>
                    </div>
                    <div class="layui-card-body">
                        <form class="layui-form">
                            @foreach($arr_model as $key=>$value)
                                @if($key == $s_model)
                                    <button class="layui-btn layui-btn-normal" name="model" value="{{$key}}">{{$value}}</button>
                                @else
                                    <button class="layui-btn layui-btn-primary" name="model" value="{{$key}}">{{$value}}</button>
                                @endif
                            @endforeach
                        </form>
                        <div>
                    <div class="layui-card-body layui_overflow_y ">
                        <div class="layui-form-item">
{{--                            <label for="username" class="layui-form-label">--}}
{{--                            <div class="zTreeDemoBackground left">--}}
{{--                                <ul id="treeDemo" class="ztree"></ul>--}}
{{--                            </div>--}}
                            <div class="layui-input-inline">
                                <ul id="zTreeDemo" class="ztree"></ul>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <div id="rMenu">
        <ul>
            <li id="m_add" onclick="addTreeNode();">增加同级权限</li>
            <li id="m_add_child" onclick="addChildTreeNode();">增加子权限</li>
            <li id="m_edit" onclick="editTreeNode();">修改权限</li>
            <li id="m_copy" onclick="copyTreeNode();">复制权限</li>
            <li id="m_open" onclick="changeTreeNode('open');">启用</li>
            <li id="m_close" onclick="changeTreeNode('close');">禁用</li>
            <li id="m_menu" onclick="changeTreeNode('menu');">设为菜单</li>
            <li id="m_point" onclick="changeTreeNode('point');">设为节点</li>
            <li id="m_del" onclick="removeTreeNode();">删除权限</li>

{{--            <li id="m_check" onclick="checkTreeNode(true);">Check节点</li>--}}
{{--            <li id="m_unCheck" onclick="checkTreeNode(false);">unCheck节点</li>--}}
{{--            <li id="m_reset" onclick="resetTree();">恢复zTree</li>--}}
        </ul>
    </div>

@endsection
@section('tab_css')
    @css(lib/zTree/css/zTreeStyle/zTreeStyle.css)
@endsection
@section('tab_js')
    @js(lib/zTree/js/jquery.ztree.core.js)
    @js(lib/zTree/js/jquery.ztree.excheck.js)
    @js(lib/zTree/js/jquery.ztree.exedit.js)

    <script>
        function open_unicode(){
            location_new('{{route('admin_unicode')}}');
        }
        /*拖拽放在 目标 前面*/
        function dropPrev(treeId, nodes, targetNode) {
            var pNode = targetNode.getParentNode();
            if (pNode && pNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    var curPNode = curDragNodes[i].getParentNode();
                    if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                        return false;
                    }
                }
            }
            return true;
        }

        /*拖拽放在 目标 里面*/
        function dropInner(treeId, nodes, targetNode) {
            if (targetNode && targetNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    if (!targetNode && curDragNodes[i].dropRoot === false) {
                        return false;
                    } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
                        return false;
                    }
                }


            }
            return true;
        }

        /*拖拽放在 目标 后边*/
        function dropNext(treeId, nodes, targetNode) {

            var pNode = targetNode.getParentNode();
            if (pNode && pNode.dropInner === false) {
                return false;
            } else {
                for (var i=0,l=curDragNodes.length; i<l; i++) {
                    var curPNode = curDragNodes[i].getParentNode();
                    if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                        return false;
                    }
                }
            }
            return true;
        }

        var log, className = "dark", curDragNodes, autoExpandNode;

        /*拖拽之前*/
        function beforeDrag(treeId, treeNodes) {

            className = (className === "dark" ? "":"dark");
            showLog("[ "+getTime()+" beforeDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes." );
            for (var i=0,l=treeNodes.length; i<l; i++) {
                if (treeNodes[i].drag === false) {
                    curDragNodes = null;
                    return false;
                } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
                    curDragNodes = null;
                    return false;
                }
            }
            curDragNodes = treeNodes;
            return true;
        }

        /*拖拽打开之前*/
        function beforeDragOpen(treeId, treeNode) {

            autoExpandNode = treeNode;
            return true;
        }

        /*放下之前*/
        function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {

            className = (className === "dark" ? "":"dark");
            showLog("[ "+getTime()+" beforeDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
            showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is "+ (isCopy==null? "cancel" : isCopy ? "copy" : "move"));
            return true;
        }

        /*拖拽操作*/
        function onDrag(event, treeId, treeNodes) {
            className = (className === "dark" ? "":"dark");
            showLog("[ "+getTime()+" onDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes." );
        }
        /*拖拽后放入操作*/
        function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {

            className = (className === "dark" ? "":"dark");

            if(moveType !== null){
                var data = {};
                data.action = moveType;
                data.drag_node_id = treeNodes[0].id;
                data.target_node_id = targetNode.id;

                //修改数据库数据
                $.ajax({
                    url: "{{route('admin_permission_drag_move_node')}}",
                    data: data,
                    type: 'post',
                    success: function (data) {
                        if (data.state == 0) {
                            layer.msg(data.msg, {icon: 6});
                        } else {
                            layer.msg(data.msg, {icon: 5});
                        }
                    },
                    error: function (msg) {
                    }
                });
            }

            // showLog("[ "+getTime()+" onDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
            // showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is "+ (isCopy==null? "cancel" : isCopy ? "copy" : "move"))
        }

        /*展开菜单*/
        function onExpand(event, treeId, treeNode) {

            if (treeNode === autoExpandNode) {
                className = (className === "dark" ? "":"dark");
                showLog("[ "+getTime()+" onExpand ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name);
            }
        }

        /*显示操作记录*/
        function showLog(str) {

            if (!log) log = $("#log");
            log.append("<li class='"+className+"'>"+str+"</li>");
            if(log.children("li").length > 8) {
                log.get(0).removeChild(log.children("li")[0]);
            }
        }
        /*获取时间*/
        function getTime() {

            var now= new Date(),
                h=now.getHours(),
                m=now.getMinutes(),
                s=now.getSeconds(),
                ms=now.getMilliseconds();
            return (h+":"+m+":"+s+ " " +ms);
        }

        function setTrigger() {

            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
        }

        /*右击显示菜单*/
        function OnRightClick(event, treeId, treeNode) {
            if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
                zTree.cancelSelectedNode();
                showRMenu("root", event.clientX, event.clientY);
            } else if (treeNode && !treeNode.noR) {
                zTree.selectNode(treeNode);
                showRMenu("node", event.clientX, event.clientY);
            }
        }

        /*显示菜单*/
        function showRMenu(type, x, y) {
            $("#rMenu ul").show();
            var select_node = zTree.getSelectedNodes()[0];
            if(select_node.status == 1){
                $("#m_open").hide();
                $("#m_close").show();

            }
            else{
                $("#m_open").show();
                $("#m_close").hide();

            }

            if(select_node.is_menu == 1){
                $("#m_menu").hide();
                $("#m_point").show();

            }
            else{
                $("#m_menu").show();
                $("#m_point").hide();

            }
            // if (type=="root") {
            //     $("#m_del").hide();
            //     $("#m_check").hide();
            //     $("#m_unCheck").hide();
            // } else {
            //     $("#m_del").show();
            //     $("#m_check").show();
            //     $("#m_unCheck").show();
            // }
            rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

            $("body").bind("mousedown", onBodyMouseDown);
        }

        /*隐藏菜单*/
        function hideRMenu() {
            if (rMenu) rMenu.css({"visibility": "hidden"});
            $("body").unbind("mousedown", onBodyMouseDown);
        }

        /*鼠标右击按下*/
        function onBodyMouseDown(event){
            if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
                rMenu.css({"visibility" : "hidden"});
            }
        }
        var addCount = 1;

        function check_form_data($_data) {
            var s_msg = '';
            // if($_data.model.length  <= 0){
            //     s_msg = '模块不能为空';
            // }
            if($_data.model.length > 20){
                s_msg = '模块不能超过20个字符啊';
                return s_msg;
            }
            if($_data.route.length  <= 0){
                s_msg = '路由名称不能为空';
                return s_msg;
            }
            if($_data.route.length > 80){
                s_msg = '路由名称不能超过80个字符啊';
                return s_msg;
            }
            if($_data.alias.length  <= 0){
                s_msg = '路由别名不能为空';
                return s_msg;
            }
            if($_data.alias.length > 80){
                s_msg = '路由别名不能超过80个字符啊';
                return s_msg;
            }
            if($_data.name.length  <= 0){
                s_msg = '权限名称不能为空';
                return s_msg;
            }
            if($_data.name.length > 80){
                s_msg = '权限名称不能超过80个字符啊';
                return s_msg;
            }
            if($_data.display_name.length > 80){
                s_msg = '权限别名不能超过80个字符啊';
                return s_msg;
            }
            if($_data.description.length > 150){
                s_msg = '权限描述不能超过150个字符啊';
                return s_msg;
            }
            if($_data.ico.length > 30){
                s_msg = '图标不能超过30个字符啊';
                return s_msg;
            }
            var int_sort = js_int($_data.sort);
            if( !((int_sort <= 32767) && (int_sort >= -32768))){
                s_msg = '排序值取值范围[-32768,32767]';
                return s_msg;
            }
            return s_msg;
        }

        /*添加顶级权限*/
        function addTopNode() {
            hideRMenu();
            var add_tree_node = layer.open({
                type: 1,
                content:$("#add_html") //这里content是一个普通的String
            });

            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    // var select_node = zTree.getSelectedNodes()[0];
                    // console.log(select_node,'select_node');
                    // console.log(zTree.getSelectedNodes(),'zTree.getSelectedNodes()');
                    // console.log(getParentNode,'getParentNode');
                    //
                    // return false;


                    //清空表单数据值
                    form.val('permission_form', {
                        id: "",
                        model: "",
                        route: "",
                        alias: "",
                        name: "",
                        display_name: "",
                        description: "",
                        ico: "",
                        sort: "",
                        pid: "0",
                        is_menu: "1",

                    });

                    //监听提交
                    form.on('submit(permission_form_submit)',
                        function(data) {
                            var $_data = data.field;
                            var msg = check_form_data($_data);
                            delete $_data.id;//添加数据是删除id属性
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('admin_permission_store')}}';
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        location.reload();
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });

                        //添加同级权限表单初始赋值
                    form.val('permission_form', {
                        "model": '{{$s_model}}',
                        "pid": 0,

                    })

                });

        }

        /*添加同级权限*/
        function addTreeNode() {
            hideRMenu();
            var add_tree_node = layer.open({
                type: 1,
                content:$("#add_html") //这里content是一个普通的String
            });

            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    var select_node = zTree.getSelectedNodes()[0];
                    // console.log(select_node,'select_node');
                    // console.log(zTree.getSelectedNodes(),'zTree.getSelectedNodes()');
                    // console.log(getParentNode,'getParentNode');
                    //
                    // return false;

                    //清空表单数据值
                    form.val('permission_form', {
                        id: "",
                        model: "",
                        route: "",
                        alias: "",
                        name: "",
                        display_name: "",
                        description: "",
                        ico: "",
                        sort: "",
                        pid: "0",
                        is_menu: "1",

                    });

                    //监听提交
                    form.on('submit(permission_form_submit)',
                        function(data) {
                            var $_data = data.field;
                            var msg = check_form_data($_data);
                            delete $_data.id;//添加数据是删除id属性
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('admin_permission_store')}}';
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                       var  newNode = result.data;

                                       //给添加的权限设置图标和组合名字
                                        if(newNode.is_menu == 1){
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_close.png";
                                        }
                                        else{
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/3.png";
                                        }
                                        newNode.real_name = newNode.name;
                                        newNode.name = newNode.name+'---'+newNode.route;
                                        layer.msg(
                                            "增加成功",
                                            {time:500,icon: 6},
                                            function() {

                                                //将新增的权限放在选中权限的父级下面-视图显示
                                                var getParentNode = select_node.getParentNode();
                                                zTree.addNodes(getParentNode, newNode);

                                                //关闭当前frame
                                                layer.close(add_tree_node);

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //添加同级权限表单初始赋值
                    form.val('permission_form', {
                        "model": select_node.model,
                        "pid": select_node.pid,

                    })

                });

        }

        /*添加子权限*/
        function addChildTreeNode() {
            hideRMenu();
            var add_tree_node = layer.open({
                type: 1,
                content:$("#add_html") //这里content是一个普通的String
            });

            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    var select_node = zTree.getSelectedNodes()[0];
                    // console.log(select_node,'select_node');
                    // console.log(zTree.getSelectedNodes(),'zTree.getSelectedNodes()');
                    // console.log(getParentNode,'getParentNode');
                    //
                    // return false;

                    //清空表单数据值
                    form.val('permission_form', {
                        id: "",
                        model: "",
                        route: "",
                        alias: "",
                        name: "",
                        display_name: "",
                        description: "",
                        ico: "",
                        sort: "",
                        pid: "0",
                        is_menu: "1",

                    });

                    //监听提交
                    form.on('submit(permission_form_submit)',
                        function(data) {
                            var $_data = data.field;
                            var msg = check_form_data($_data);
                            delete $_data.id;//添加数据是删除id属性
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('admin_permission_store')}}';
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        var  newNode = result.data;

                                        //给添加的权限设置图标和组合名字
                                        if(newNode.is_menu == 1){
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_close.png";
                                        }
                                        else{
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/3.png";
                                        }
                                        newNode.real_name = newNode.name;
                                        newNode.name = newNode.name+'---'+newNode.route;
                                        layer.msg(
                                            "增加成功",
                                            {time:500,icon: 6},
                                            function() {

                                                //将新增的权限放在选中权限的下面-视图显示
                                                zTree.addNodes(select_node, newNode);

                                                //关闭当前frame
                                                layer.close(add_tree_node);

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //添加子级权限表单初始赋值
                    form.val('permission_form', {
                        "model": select_node.model,
                        "pid": select_node.id,

                    })

                });

        }

        /*复制权限*/
        function copyTreeNode(){
            hideRMenu();
            var add_tree_node = layer.open({
                type: 1,
                content:$("#add_html") //这里content是一个普通的String
            });

            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    var select_node = zTree.getSelectedNodes()[0];
                    // console.log(select_node,'select_node');
                    // console.log(zTree.getSelectedNodes(),'zTree.getSelectedNodes()');
                    // console.log(getParentNode,'getParentNode');
                    //
                    // return false;

                    //清空表单数据值
                    form.val('permission_form', {
                        id: "",
                        model: "",
                        route: "",
                        alias: "",
                        name: "",
                        display_name: "",
                        description: "",
                        ico: "",
                        sort: "",
                        pid: "0",
                        is_menu: "1",

                    });

                    //监听提交
                    form.on('submit(permission_form_submit)',
                        function(data) {
                            var $_data = data.field;
                            var msg = check_form_data($_data);
                            delete $_data.id;//添加数据是删除id属性
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('admin_permission_store')}}';
                            $.ajax({
                                url:$_url,
                                type:'post',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        var  newNode = result.data;

                                        //给添加的权限设置图标和组合名字
                                        if(newNode.is_menu == 1){
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_close.png";
                                        }
                                        else{
                                            newNode.icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/3.png";
                                        }
                                        newNode.real_name = newNode.name;
                                        newNode.name = newNode.name+'---'+newNode.route;
                                        layer.msg(
                                            "增加成功",
                                            {time:500,icon: 6},
                                            function() {

                                                //将新增的权限放在选中权限的父级下面-视图显示
                                                var getParentNode = select_node.getParentNode();
                                                zTree.addNodes(getParentNode, newNode);

                                                //关闭当前frame
                                                layer.close(add_tree_node);

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //复制同级权限表单初始赋值
                    select_node.name = select_node.real_name;
                    form.val('permission_form', select_node)

                });

        }

        /*编辑权限*/
        function editTreeNode() {
            hideRMenu();
            var edit_tree_node = layer.open({
                type: 1,
                content:$("#add_html") //这里content是一个普通的String
            });

            layui.use(['form', 'layer','jquery'],
                function() {
                    $ = layui.jquery;
                    var form = layui.form,
                        layer = layui.layer;
                    var select_node = zTree.getSelectedNodes()[0];
                    // console.log(select_node,'select_node');
                    // console.log(zTree.getSelectedNodes(),'zTree.getSelectedNodes()');
                    // console.log(getParentNode,'getParentNode');
                    //
                    // return false;

                    //清空表单数据值
                    form.val('permission_form', {
                        id: "",
                        model: "",
                        route: "",
                        alias: "",
                        name: "",
                        display_name: "",
                        description: "",
                        ico: "",
                        sort: "",
                        pid: "0",
                        is_menu: "1",

                    });

                    //监听提交
                    form.on('submit(permission_form_submit)',
                        function(data) {
                            var $_data = data.field;
                            var msg = check_form_data($_data);
                            if(msg !== ''){
                                layer.msg(msg);
                                return false;
                            }

                            var $_url = '{{route('admin_permission_update')}}';
                            $.ajax({
                                url:$_url,
                                type:'put',
                                data:$_data,
                                dataType : "json",
                                success:function(result){
                                    if(result.state === 0){
                                        var  newNode = result.data;
                                        var icon = '';

                                        //给修改的权限设置图标和组合名字
                                        if(newNode.is_menu == 1){
                                            icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_close.png";
                                        }
                                        else{
                                            icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/3.png";
                                        }
                                        var real_name = newNode.name;
                                        var name = newNode.name +'---'+newNode.route;
                                        select_node.icon = icon;
                                        select_node.real_name = real_name;
                                        select_node.name = name;
                                        layer.msg(
                                            "修改成功",
                                            {time:500,icon: 6},
                                            function() {

                                                //将修改的权限-视图显示
                                                zTree.updateNode(select_node);

                                                //关闭当前frame
                                                layer.close(edit_tree_node);

                                            });
                                    }else{
                                        layer.msg(result.msg, {time:2000,icon: 5});
                                    }

                                }});
                            return false;

                        });
                    //修改权限表单初始赋值
                    select_node.name = select_node.real_name;
                    form.val('permission_form', select_node)

                });

        }

        /*修改权限状态*/
        function changeTreeNode($_type) {
            var s_msg = '';
            var data = {};
            var select_node = zTree.getSelectedNodes()[0];
            switch ($_type) {
                case 'open':
                    s_msg = '确认要启用权限？';
                    data.status = 1;
                    break;
                case 'close':
                    s_msg = '确认要禁用权限？';
                    data.status = 2;
                    break;
                case 'menu':
                    s_msg = '确认要设置权限为菜单？';
                    data.is_menu = 1;

                    break;
                case 'point':
                    s_msg = '确认要设置权限为节点？';
                    data.is_menu = 2;
                    break;
                default:
                    s_msg = '';
            }
            hideRMenu();
            var nodes = zTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                layer.confirm(s_msg, {
                    btn: [ '确定','取消'] //可以无限个按钮
                }, function(index, layero){
                    permission_change(data)
                }, function(index){
                });
            }

            function permission_change(data) {
                data.id = nodes[0].id;
                if(!data.status){
                    data.status = nodes[0].status;
                }
                if(!data.is_menu){
                    data.is_menu = nodes[0].is_menu;
                }
                //修改权限状态
                var $_url = '{{route('admin_permission_update')}}';
                $.ajax({
                    url:$_url,
                    type:'put',
                    data:data,
                    dataType : "json",
                    success:function(result){
                        if(result.state === 0){
                            var  newNode = result.data;
                            var icon = '';
                            if(newNode.is_menu == 1){
                                //                $value['iconOpen'] = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_open.png";
                                //                $value['iconClose'] = "/admin/lib/zTree/css/zTreeStyle/img/diy/1_close.png";
                                if(newNode.status == 1){
                                    icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/menu.png";
                                }
                                else{
                                    icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/menu_disabled.png";
                                }
                            }
                            else{
                                if(newNode.status == 1){
                                    icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/point.png";
                                }
                                else{
                                    icon = "/admin/lib/zTree/css/zTreeStyle/img/diy/point_disabled.png";
                                }
                            }
                            select_node.icon = icon;
                            select_node.status = newNode.status;
                            select_node.is_menu = newNode.is_menu;

                            layer.msg(
                                "修改成功",
                                {time:500,icon: 6},
                                function() {
                                    //将修改的权限-视图显示
                                    zTree.updateNode(select_node);

                                });
                        }else{
                            layer.msg(result.msg, {time:2000,icon: 5});
                        }

                    }});
            }
        }

        /*删除权限*/
        function removeTreeNode() {
            hideRMenu();
            var nodes = zTree.getSelectedNodes();
            if (nodes && nodes.length>0) {
                if (nodes[0].children && nodes[0].children.length > 0) {
                    layer.confirm('要删除的节点是父节点，如果删除将连同子节点一起删掉？', {
                        btn: ['确定删除', '取消'] //可以无限个按钮
                    }, function(index, layero){
                        permission_delete()
                    }, function(index){
                    });
                    // var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
                    // if (confirm(msg)==true){
                    //     go = true;
                    // }
                } else {
                    layer.confirm('确认要删除节点？', {
                        btn: [ '确定删除','取消'] //可以无限个按钮
                    }, function(index, layero){
                        permission_delete()
                    }, function(index){
                    });
                }


            }

            /*删除权限*/
            function permission_delete(){
                //删除权限
                var $_url = '{{route('admin_permission_delete')}}';
                $.ajax({
                    url:$_url,
                    type:'delete',
                    data:{id:nodes[0].id},
                    dataType : "json",
                    success:function(result){
                        if(result.state === 0){

                            layer.msg(
                                "删除成功",
                                {time:500,icon: 6},
                                function() {
                                    //删除权限视图
                                    zTree.removeNode(nodes[0]);

                                });
                        }else{
                            layer.msg(result.msg, {time:2000,icon: 5});
                        }

                    }});
            }
        }

        /*加载菜单树*/
        var zTree, rMenu;
        $(document).ready(function(){

            /*初始化配置*/
            var setting = {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pid",
                        rootPId: 0
                    }
                },
                view: {
                    // showIcon: false
                    // dblClickExpand: false

                },

                edit: {
                    drag: {
                        autoExpandTrigger: true,//拖拽时父节点自动展开是否触发 onExpand 事件回调函数。[setting.edit.enable = true 时生效] 默认值：false
                        prev: dropPrev,//拖拽到目标前面
                        inner: dropInner,//拖拽到目标里面
                        next: dropNext//拖拽到目标后面
                    },
                    enable: true,//设置 zTree 是否处于编辑状态 true / false 分别表示 可以 / 不可以 编辑
                    showRemoveBtn: false,
                    showRenameBtn: false
                    // showRemoveBtn: true,
                    // removeTitle: "删除节点",
                    // showRenameBtn: true,
                    // renameTitle: "编辑节点名称"
                },
                check: {
                    // enable: true
                },

                callback: {
                    beforeDrag: beforeDrag,
                    beforeDrop: beforeDrop,
                    beforeDragOpen: beforeDragOpen,
                    onDrag: onDrag,
                    onDrop: onDrop,
                    onExpand: onExpand,

                    onRightClick: OnRightClick

                }
            };

            /*初始化节点信息*/
            var zNodes = '{!! $arr_permission !!}';
            zNodes =json_string_to_obj(zNodes);
            $.fn.zTree.init($("#zTreeDemo"), setting, zNodes);
            zTree = $.fn.zTree.getZTreeObj("zTreeDemo");
            rMenu = $("#rMenu");
            $("#callbackTrigger").bind("change", {}, setTrigger);

        });

        layui.use(['form'], function(){
           var form = layui.form;

            //监听指定开关-修改状态
            form.on('switch(change_state)',
                function(obj){
                    change_state(this);
                // layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                //     offset: '60px'
                // });
                // layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', obj.othis)
            });
        });



    </script>
    <style type="text/css">
        div#rMenu {
            position:absolute;
            visibility:hidden;
            top:0;
            /*background-color: #1E9FFF;*/
            background-color: #555;

            text-align: left;
            padding:0 2px;
        }
        div#rMenu ul li{
            margin: 2px 0;
            padding: 0 5px;
            line-height: 25px;
            cursor: pointer;
            list-style: none outside none;
            background-color: #f3f3f3;
        }
    </style>
@endsection




